const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tstatus: true,

    page: 1,
    limit: 10,
    type:0,
    student_id:'',
    bookbags_id:'',

    typelist:[],
    list: [],
  },

  // 确认推荐
  defind:function(){
    wx.showLoading({
      title: '加载中'
    })
    var that = this
    if(that.data.bookbags_id == ''){
      wx.showToast({
        title: '请选择书包编号',
        icon:'none',
        duration: 2000
      })
      return false;
    }
    wx.getStorage({
      key: 'user',
      success: function(res) {
        console.log(res)
        let setdata = {
          token:res.data.token,
          student_id: that.data.student_id,
          bookbags_id: that.data.bookbags_id,
        }
        console.log(setdata)
        wx.request({
          url: app.api + app.lendbag,
          method:'POST',
          data:setdata,
          success(e){
            wx.hideLoading()
            console.log(e);
            if(e.data.code == 0){
              wx.showToast({
                title: e.data.msg,
                icon:'none',
                duration:2000,
                success(){
                  setTimeout(()=>{
                    wx.reLaunch({
                      url: '../jy/jy',
                    })
                  },2000)
                  
                }
              })
              
            }else if(e.data.code == -1){
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000,
                success() {
                  wx.redirectTo({
                    url: '../login/login',
                  })
                }
              })
            }else{
              wx.showToast({
                title: e.data.msg,
                icon:'none',
                duration:2000
              })
              
            }
          }
        })
      },
    })
  },
  bookbagid:function(e){
    console.log(e)
    if (this.data.bookbags_id == e.currentTarget.dataset.bookbagsid){
      this.setData({
        bookbags_id:''
      })
    }else{
      this.setData({
        bookbags_id: e.currentTarget.dataset.bookbagsid
      })
    }
    
  },


  tabli:function(e){
    var that = this
    console.log(e)
    that.setData({
      type: e.currentTarget.dataset.index,
      page: 1,
      list: [],
    })
    that.tjgetlist()
  },
  /**
   * 生命周期函数--监听页面加载
   */

  //推荐阅读
  tjgetlist: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        console.log(res);
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
          type: that.data.type,
          student_id:that.data.student_id
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.bookbags,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list,
                  typelist:e.data.data.type
                })
                console.log("aaa")
                wx.hideLoading();
                // 空的
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon:'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let list = that.data.list
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
                wx.hideLoading()
                console.log(list)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon:'none',
                  duration: 2000
                })
              }

              
            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  onLoad: function (options) {
    var that = this
    console.log(options)
    that.setData({
      student_id:options.student_id
    })
    that.tjgetlist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      page:that.data.page + 1
    })
    that.tjgetlist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})